package StudyCode.summary.mycollection;

import java.util.Arrays;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class AddAnElementToAnArray {
	public static void main(String[] argv) {
		String[] array = new String[]{"1", "abc", "level", null,
				"www.tah1986.com", "asdf 123"};
		String s = "www.tah1986.com";
		System.out.println("数组添加元素后:");
		System.out.println(Arrays.toString(addToArray(array, s)));
	}

	public static String[] addToArray(String[] array, String s) {
		String[] ans = null;
		if (0 == array.length) {
			ans = new String[1];
		} else {
			ans = new String[array.length + 1];
		}
		System.arraycopy(array, 0, ans, 0, array.length);
		ans[ans.length - 1] = s;
		return ans;
	}

	public static int[] addToArray(int[] array, int val) {
		int[] ans = null;
		if (0 == array.length) {
			ans = new int[1];
		} else {
			ans = new int[array.length + 1];
		}
		System.arraycopy(array, 0, ans, 0, array.length);
		ans[ans.length - 1] = val;
		return ans;
	}
}
